package com.ysq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ysq.controller.result.Result;
import com.ysq.entity.Employee;
import com.ysq.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: EmployeeController
 * Package: com.ysq.controller
 * Description:
 *
 * @Author YSQ
 * @Create 2023/1/2 - 8:45
 */
@Slf4j //方便发日志测试
@RestController
@RequestMapping("/employee") //因为前端发送的路径就是employee
public class EmployeeController {
    @Autowired
    private EmployeeService service;

    /**
     * 员工登录
     * @param request 接收request对象, 这样可以获取session存储登录员工信息
     * @param employee 前端发送的登录数据封装到了employee对象中, 对应属性为username和password
     * @return 统一返回结果封装类Result
     */
    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //执行登录业务
        Result<Employee> result = service.login(employee);
        if (result.getCode() == 1) {
            //登录成功, 将员工id存入Session
            request.getSession().setAttribute("employee", result.getData().getId());
        }
        //返回结果
        return result;
    }

    /**
     * 员工退出
     * @param request 接收request对象, 通过request对象获取session清除之前登录成功保存的员工信息
     * @return 统一返回结果封装类Result
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    /**
     * 添加员工
     * @param request 用于设置部分属性
     * @param employee 接收前端发送的员工信息
     * @return 统一返回结果封装类Result
     */
    @PostMapping
    public Result<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        boolean flag = service.save(request, employee);
        if (flag) {
            return Result.success("新增员工成功");
        }
        return Result.error("新增员工失败");
    }

    /**
     * 员工信息分页查询 + 模糊查询
     * @param page 第几页数据
     * @param pageSize 一页几条数据
     * @param name 模糊查询的条件
     * @return 返回统一封装类, data属性封装page
     */
    @GetMapping("/page")
    public Result<Page<Employee>> page(int page, int pageSize, String name) {
        Page<Employee> employeePage = service.selectPage(page, pageSize, name);
        return Result.success(employeePage);
    }

    /**
     * 根据ID更新用户信息
     * @param request 用来获取当前登录用户信息
     * @param employee 接收前端传来的员工信息
     * @return 返回统一封装类
     */
    @PutMapping
    public Result<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        boolean flag = service.update(request, employee);
        if (flag) {
            return Result.success("员工信息更新成功");
        }
        return Result.error("员工信息更新失败");
    }

    /**
     * 员工信息编辑界面, 要完成数据回显
     * @param id 接收前端传来的员工ID
     * @return 返回统一封装类
     */
    @GetMapping("/{id}")
    public Result<Employee> edit(@PathVariable Long id) {
        Employee employee = service.dataEcho(id);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("没有查到对应员工信息~~~");
    }
}
